package com.nextel.project.util;

import com.nextel.project.reporte.mvc.validate.ValidateReporteForm;
import com.nextel.ui.Key;
import com.nextel.ui.OGrid;
import com.nextel.ui.OGridInfo;
import com.nextel.ui.OGridObjectRow;
import com.nextel.ui.OSoftKey;
import com.nextel.ui.OUILook;
import javax.microedition.lcdui.Graphics;
import pe.com.nextel.main.MVCAction;
import pe.com.nextel.rms.Persistente;
import pe.com.nextel.util.List;

/**
 *
 * @author eespinoza
 */
public abstract class MVCBasicTabla extends MVCAction  { 
  
    private OGrid  table;
    private OSoftKey cmdSeguir, cmdAtras;
    protected List lista;
    protected boolean flagForm; // para que no presione dos veces al momento de buscar

    private String tituloForm;
    private String backtitulo;
    private String siguetitulo;
    
    public MVCBasicTabla(String titulo, String nameSigue, String nameback) {

        this.tituloForm = titulo;
        this.backtitulo = nameback;
        this.siguetitulo = nameSigue;
 
    }
    
    protected void destroy() {
        
        super.destroy();
        
        if (this.lista != null) {
            this.lista.clear();
            this.lista = null;
        }
        
 
    }
    
    protected void createView()  {
        
        
        cmdSeguir = new OSoftKey(siguetitulo );
        cmdAtras = new OSoftKey(backtitulo );
 
        formulario.setTitle(tituloForm, OUILook.PLAIN_SMALL_TEXT);
       
        formulario.addSoftKey(cmdSeguir);
        
        formulario.addSoftKey(cmdAtras);
        
        crearBotonesAdicionales( );
        
    }
    
    protected void createItems() {
        
        crearLista();
        flagForm = true;
    }
    
    
    public void crearLista( ){

        this.lista =  getDetalleTabla( );
 
        int numberOfLines = 5;
        int numberOfColumns = getCabeceraTabla( ).length;
        
        int columnWidths[] = {formulario.getWidth()/2, formulario.getWidth()/2,formulario.getWidth()/2,formulario.getWidth()/2};
        
        int columnAlignments[] = {Graphics.LEFT, Graphics.HCENTER,Graphics.RIGHT,Graphics.RIGHT};
       
        OGridInfo gridInfo = new OGridInfo(OGridInfo.GRID_BORDER_TYPE_BOTH,
                numberOfLines,
                numberOfColumns,
                null,
                columnAlignments,
                columnWidths,
                formulario.getWidth(),
                OUILook.PLAIN_SMALL_TEXT,
                OUILook.PLAIN_SMALL_TEXT, OUILook.PLAIN_SMALL_TEXT);

        OGrid grid = new OGrid(gridInfo);
 
        OGridObjectRow items[] = new OGridObjectRow[lista.size( )];

        for (int index=0; index < lista.size( ); index++) {
            String[] detalle =  (String[]) lista.get(index);
            OGridObjectRow row = new OGridObjectRow(numberOfColumns);
 
            for(int ivalor=0; ivalor<getCabeceraTabla( ).length; ivalor++)
            {
                row.addCell(ivalor, detalle[ivalor]);
            }

            items[index] = row;
        }
        grid.setViewableWidth(formulario.getWidth());
        grid.populate( items );
        ValidateReporteForm vrf = new ValidateReporteForm(this);
        grid.setTableListener(vrf);
        grid.setColumnHeadings(getCabeceraTabla( ), OUILook.PLAIN_SMALL_TITLE);

        
        formulario.add(grid);
 
    }
 
     public void performAction(OSoftKey c) {
        if (flagForm) {
            
            if (c == cmdAtras) {
                flagForm = false;
                destroy();
                atras();
            }else if (c == cmdSeguir) {
                flagForm = false;
                
                if (this.lista != null && lista.size( ) > 0 ) { 
                    seguir( );
                    destroy( );
                } else {
                    flagForm = true;
                }
                
            }
            
            botonesClickAdicional(c);
        }
    }
 
    public void performKey(int indx) {
        if (flagForm) {
            if (indx != Key.Key_OK) {
                return;
            }
            flagForm = false;
            if (lista.size( ) > 0) {
                seguir();
                destroy();
            } else {
                flagForm = true;
            }
        }
    }
    
    public Persistente escoger() {
 
        Object obj = lista.get(table.getSelectedIndex());
 
        return (Persistente) obj;
    }
    
    public abstract void crearBotonesAdicionales( );
    
    public abstract void botonesClickAdicional(OSoftKey c);
    
    public abstract String[] getCabeceraTabla( );
    
    public abstract List getDetalleTabla();
    
    public abstract void seguir();
    
    public abstract void atras();
    
}
